package com.bdqn.controller;

import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import com.bdqn.util.Constants;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
 * 12章示例26
 */
public class LoginController {
    private Logger logger=Logger.getLogger(LoginController.class);
    @Resource
    private SysUserService sysUserService;
    @RequestMapping(value = "/toLogin")
    public String toLogin(){
        return "login";
    }

    //登录接口
    @RequestMapping("/login")
    public String doLogin(@RequestParam String account, @RequestParam String password,
                          HttpServletRequest request, HttpSession session)throws Exception{
        SysUser sysUser=sysUserService.login(account,password);
        if(null!=sysUser){
            session.setAttribute(Constants.USER_SESSION,sysUser);
            return "redirect:/toMain";
        }else{
            request.setAttribute("error","用户名或密码错误");
            return "login";
        }
    }
    // 退出
    @RequestMapping("/logout")
    public String tui(HttpSession session){
        session.removeAttribute("USER_SESSION");
        session.invalidate();
        return "login";
    }

    @RequestMapping("/toMain")
    public String toMain(){
        return "frame";
    }

}
